<template>
    <div>
        <Upload :action="picurl" :headers="headers" :on-success="handleSuccess" :on-format-error="handleFormatError" :before-upload="handleBeforeUpload">
            <Button type="ghost" icon="ios-cloud-upload-outline">上传文件</Button>
        </Upload>
    </div>
</template>
<script>
export default {
    data () {
        return {
            headers: {
                'Authorization': 'Bearer '+((localStorage.token)?(localStorage.token):'')
            },
            picurl: this.$project.axios.baseUrl + 'api/user/v1/upload',
            status: true,
        }
    },
    methods: {

        // ==================================================
        // 上传文件成功时
        // ==================================================
        handleSuccess (response, file, fileList) {
            this.status = false
            this.$emit('on-upload-files',response.data)
        },

        // ==================================================
        // 上传文件失败时
        // ==================================================
        handleFormatError (file, fileList) {
            this.$Notice.warning({
                title: '上传文件失败时'
            });
        },

        // ==================================================
        // 上传文件前
        // ==================================================
        handleBeforeUpload () {
            const check = this.status
            if (!check) {
                this.$Notice.warning({
                    title: '只能上传一个文件！'
                });
            }
            return check;
        },
    },
    mounted () {
    },
    created(){
    }
};
</script>